import random
import matplotlib.pyplot as plt

# 模拟投掷骰子10000次
num_rolls = 10000
dice_results = {i: 0 for i in range(1, 7)}  # 初始化字典，键为1-6，值为0

for _ in range(num_rolls):
    roll = random.randint(1, 6)
    dice_results[roll] += 1

# 准备绘制图表
values = list(dice_results.values())  # 点数出现的次数
keys = list(dice_results.keys())  # 点数1-6

# 绘制条形图
plt.bar(keys, values, color='skyblue')

# 设置标题和标签
plt.title('Results of 10000 Dice Rolls')
plt.xlabel('Dice Value')
plt.ylabel('Frequency')

# 显示图表
plt.show()

# 保存图表为图片
plt.savefig('dice_rolls_results.png')